﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class FormHocKy : QuanLyHocPhi.GUI.FormBaseDialog
    {
        public FormHocKy()
        {
            InitializeComponent();
            
            gridHocKy.ReadOnly = true;
            gridHocKy.AllowUserToAddRows = false;
            gridHocKy.AllowUserToDeleteRows = false;
            gridHocKy.AutoGenerateColumns = false;
            colHocKy_MaHocKy.DataPropertyName = "MaHocKy";
            colHocKy_NamHoc.DataPropertyName = "NamHoc";
            colHocKy_SoHieuHocKy.DataPropertyName = "SoHieuHocKy";
            colHocKy_HocPhiChuyen.DataPropertyName = "HocPhiChuyen";
            colHocKy_PhuThuChuyen.DataPropertyName = "PhuThuChuyen";
            colHocKy_HocPhiKhongChuyen.DataPropertyName = "HocPhiKhongChuyen";
            colHocKy_PhuThuKhongChuyen.DataPropertyName = "PhuThuKhongChuyen";
            colHocKy_TrangThaiHoatDong.DataPropertyName = "TrangThaiHoatDong";
            DataGridViewCellStyle cellstyleHocPhi = new DataGridViewCellStyle();
            cellstyleHocPhi.Format = "N0";
            colHocKy_HocPhiChuyen.DefaultCellStyle =
                colHocKy_PhuThuChuyen.DefaultCellStyle =
                colHocKy_HocPhiKhongChuyen.DefaultCellStyle =
                colHocKy_PhuThuKhongChuyen.DefaultCellStyle = cellstyleHocPhi;
        }

        private string SelectedNamHoc
        {
            get
            {
                if (cbxNamHoc.SelectedItem == null)
                    return null;
                else
                    return cbxNamHoc.SelectedItem.ToString();
            }
        }

        private HocKy SelectedHocKy
        {
            get
            {
                if (bsHocKy.DataSource == null || bsHocKy.Current == null)
                    return null;
                else
                    return (bsHocKy.Current as HocKy);
            }
        }

        private void OnLoad(object sender, EventArgs e)
        {
            btnHocKy_Reload.PerformClick();
        }

        private void NapDanhSachNamHoc(string selectedNamHoc)
        {
            var lstNamHoc = BizHocKy.GetList_NamHoc();
            cbxNamHoc.DataSource = lstNamHoc;

            if (selectedNamHoc != null)
            {
                for (int i = 0; i < cbxNamHoc.Items.Count; i++)
                    if (cbxNamHoc.Items[i].ToString() == selectedNamHoc)
                    {
                        cbxNamHoc.SelectedIndex = i;
                        break;
                    }
            }
        }

        private void NapDanhSachHocKy(string namHoc)
        {
            if (namHoc.IsNullOrEmpty())
                bsHocKy.DataSource = null;
            else
            {
                var lstHocKy = BizHocKy.GetList_HocKy(namHoc);
                bsHocKy.DataSource = lstHocKy;
            }

            gridHocKy.DataSource = bsHocKy;
        }

        private void OnAddNew_HocKyClick(object sender, EventArgs e)
        {
            HocKy hocKy = BizHocKy.GetDefaultNew_HocKy();
            FormHocKy_Details frm = new FormHocKy_Details(hocKy: hocKy, isAdding: true);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                this.OnLoad(null);
        }

        private void OnReload_HocKyClick(object sender, EventArgs e)
        {
            this.LockUpdate();
            NapDanhSachNamHoc(this.SelectedNamHoc);
            NapDanhSachHocKy(this.SelectedNamHoc);
            this.UnlockUpdate();
        }

        private void OnEdit_HocKyClick(object sender, EventArgs e)
        {
            var hocKy = this.SelectedHocKy;
            if (hocKy != null)
            {
                FormHocKy_Details frm = new FormHocKy_Details(hocKy: hocKy, isAdding: false);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    this.OnLoad(null);
            }
        }

        private void OnDeleteHocKyClick(object sender, EventArgs e)
        {
            if (DialogUtils.ShowYesNo("Bạn có chắc chắn muốn xoá học kỳ đang chọn không?") != System.Windows.Forms.DialogResult.Yes)
                return;
            var hocKy = this.SelectedHocKy;
            if (hocKy != null)
            {
                try
                {
                    BizHocKy.Delete_HocKy(hocKy);
                    this.OnLoad(null);
                }
                catch (Exception exc)
                {
                    DialogUtils.ShowError(exc.Message);
                }
            }
        }

        #region Cập nhật thông tin học kỳ đang chọn
        private void OnBsHocKyCurrentChanged(object sender, EventArgs e)
        {
            CapNhatTrangThaiHocKyDangChon();
        }

        private void CapNhatTrangThaiHocKyDangChon()
        {
            HocKy selectedHocKy = this.SelectedHocKy;
            btnHocKy_Delete.Enabled =
                btnHocKy_Edit.Enabled = selectedHocKy != null;
        }

        #endregion

        private void OnCbxNamHocSelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_LockUpdate)
                return;
            NapDanhSachHocKy(this.SelectedNamHoc);
        }

        private void OnMenuHocKy_SaoChepTuHocKyKhacClick(object sender, EventArgs e)
        {

        }

        private void OnGridHocKyCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
                btnHocKy_Edit.PerformClick();
        }
    }
}
